package com.woniu.payback_font.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.payback_font.entity.Account;
import com.woniu.payback_font.mapper.AccountMapper;
import com.woniu.payback_font.mapper.UserMessageMapper;
import com.woniu.payback_font.service.UserMessageService;
import com.woniu.payback_font.vo.IsOkVo;
import com.woniu.payback_font.vo.UserMessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class UserMessageServiceImpl implements UserMessageService {

    @Autowired
    UserMessageMapper userMessageMapper;
    @Autowired
    AccountMapper aanagerMapper;

    /**
     * 多条件查询用户信息表字段数据
     * @param umName
     * @param uMobile
     * @param umIdcard
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<UserMessageVo> findByNameAndModilAndidCrid(String umName, String uMobile, String umIdcard, Integer pageNum, Integer pageSize) {
        //启动分页插件
        PageHelper.startPage(pageNum, pageSize);
        List<UserMessageVo> list = userMessageMapper.findByNameAndModilAndidCrid(umName, uMobile, umIdcard);
        return new PageInfo<>(list);
    }

    /**
     * 查询单条数据
     * @param umId
     * @return
     */
    @Override
    public UserMessageVo findOneUserManager(Integer umId) {
        return userMessageMapper.findOneUserManager(umId);
    }

    /**
     * 拒绝审批
     * @param isOkVo
     * @return
     */
    @Override
    public int userNO(IsOkVo isOkVo) {
        return userMessageMapper.upuserMessage(isOkVo);
    }

    /**
     * 审核通过（开启事务支持）
     * @param isOkVo
     * @return
     */
    @Transactional
    @Override
    public int userOk(IsOkVo isOkVo) {

        int i = 0;//更新数据库表字段
        try {
            i = userMessageMapper.upuserMessage(isOkVo);
            UserMessageVo usermanagervo = userMessageMapper.findOneUserManager(isOkVo.getUmId());//得到当前更新的字段
            Account aanager = new Account();
            aanager.setaUid(usermanagervo.getUmUid());//插入用户唯一标识
            aanager.setaName(usermanagervo.getUmName());//插入用户名
            aanager.setaMobile(usermanagervo.getuMobile());//插入手机号
            aanager.setaIdcar(usermanagervo.getUmIdcard());//插入身份证
            aanager.setaProductName("戴去花");
            int rNum = 100000+(int)(Math.random()*(200000+1));//随机生成一个区间内的信用额度
            aanager.setaCreditLimit(BigDecimal.valueOf(rNum));
            aanager.setaUsedLimit(BigDecimal.valueOf(0));//已用额度初始为0
            aanager.setaDebtMoney(BigDecimal.valueOf(0));//欠款金额初始为0
            aanager.setaDebtCapita(BigDecimal.valueOf(0));//欠款本金初始为0
            aanager.setaDebtInterest(BigDecimal.valueOf(0));//利息初始为0
            aanager.setaContractStartTime(usermanagervo.getUmInsertTime());//合同开始时间即实名认证时间
            Date date = usermanagervo.getUmInsertTime();
            date.setYear(date.getYear()+5);
            aanager.setaContractEndTime(date);//合同结束时间默认5年
            aanager.setaInvestorName("蜗牛消费金融");
            aanager.setaContractStatus(1);
            i+= aanagerMapper.insert(aanager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }




}
